package org.six11.ss.client;

import java.awt.BorderLayout;
import java.awt.LayoutManager;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.List;
import java.util.SortedSet;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

import org.six11.util.Debug;
import org.six11.util.adt.MultiState;

/**
 * 
 * 
 * @author Gabe Johnson <johnsogg@cmu.edu>
 */
public class ScorePanel extends JPanel {

  private MultiState state;
  private JTable table;

  public ScorePanel(MultiState s) {
    super();
    this.state = s;
    this.table = new JTable();
    JScrollPane tableScroller = new JScrollPane(table);
    setLayout(new BorderLayout());
    add(tableScroller, BorderLayout.CENTER);
    state.addChangeListener(ClientStateNames.K_SCORES, new PropertyChangeListener() {
      @SuppressWarnings("unchecked")
      public void propertyChange(PropertyChangeEvent evt) {
        SortedSet<ScoreInfo> data = (SortedSet<ScoreInfo>) evt.getNewValue();
        if (data != null) {
          setScores(data);
        }
      }
    });
  }

  protected void setScores(SortedSet<ScoreInfo> scores) {
//    ScoreTableModel newTableModel = new ScoreTableModel(scores);
//    table.setModel(newTableModel);
//    for (int i = 0; i < newTableModel.getColumnCount(); i++) {
//      bug("column " + i + " is named " + newTableModel.getColumnName(i));
//      table.getColumnModel().getColumn(i).setHeaderValue(newTableModel.getColumnName(i));
//    }
//    revalidate();
  }

  protected static class ScoreTableModel extends AbstractTableModel {
    int colCount = 1;
    int rowCount = -1;
    int nameIdx = 0;
    int lifeIdx = -1;
    int gameIdx = -1;
    int roundIdx = -1;
    List<ScoreInfo> data;

    ScoreTableModel(SortedSet<ScoreInfo> scores) {
      this.data = new ArrayList<ScoreInfo>();
      data.addAll(scores);
      colCount = 1;
      if (data.size() > 0) {
        rowCount = data.size();
        ScoreInfo info = data.get(0);
        if (info.getLifetimeScore() >= 0) {
          lifeIdx = colCount;
          colCount++;
        }
        if (info.getGameScore() >= 0) {
          gameIdx = colCount;
          colCount++;
        }
        if (info.getRoundScore() >= 0) {
          roundIdx = colCount;
          colCount++;
        }
      }
    }

    public int getColumnCount() {
      return colCount;
    }

    public int getRowCount() {
      return rowCount;
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
      ScoreInfo s = data.get(rowIndex);
      String ret = "";
      if (columnIndex == nameIdx) {
        ret = s.getPlayerName();
      } else if (columnIndex == lifeIdx) {
        ret = s.getLifetimeScore() + "";
      } else if (columnIndex == gameIdx) {
        ret = s.getGameScore() + "";
      } else if (columnIndex == roundIdx) {
        ret = s.getRoundScore() + "";
      }
      return ret;
    }

    public Class getColumnClass(int columnIndex) {
      return String.class;
    }

    public String getColumnName(int columnIndex) {
      String ret = "";
      if (columnIndex == nameIdx) {
        ret = "Player Nickname";
      } else if (columnIndex == lifeIdx) {
        ret = "Lifetime";
      } else if (columnIndex == gameIdx) {
        ret = "This Game";
      } else if (columnIndex == roundIdx) {
        ret = "This Round";
      }
      return ret;
    }
  }

  private static void bug(String what) {
    Debug.out("ScorePanel", what);
  }
}
